package com.demo.core.service.base.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.dao.OrgDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Function;
import com.demo.core.model.Org;
import com.demo.core.service.base.OrgService;
/**
 * 机构服务实现类
 * @author ryuu.kk
 *
 */
public class OrgServiceImpl implements OrgService {

	/**
	 * 机构Dao
	 */
	private OrgDao orgDao;
	
	@Override
	public Long save(Org t) {
		return orgDao.insert(t);
	}

	@Override
	public boolean exists(Long id) {
		return orgDao.exists(id);
	}

	@Override
	public void delete(Long id) {
		orgDao.delete(id);
	}
	@Override
	public void updateArea(Long id, Long areaId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		Org org = new Org();
		org.setAreaId(areaId);
		orgDao.updateArea(org, params);
	}
	@Override
	public void updateEnabled(Long id, boolean enabled, Date expireDay) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		Org org = new Org();
		org.setEnabled(enabled);
		org.setExpireDay(expireDay);
		orgDao.updateStatus(org, params);
	}
	@Override
	public void updateType(Long id, String type) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		Org org = new Org();
		org.setType(type);
		orgDao.update(org, params);
	}
	@Override
	public void updateName(Long id, String name) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		Org org = new Org();
		org.setName(name);
		orgDao.update(org, params);
	}
	@Override
	public void updateCode(Long id, String code) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		Org org = new Org();
		org.setCode(code);
		orgDao.update(org, params);
	}
	@Override
	public Org find(Long id) {
		return orgDao.select(id);
	}

	@Override
	public List<Org> findAll(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("siteId", siteId);
		return orgDao.select(params);
	}

	@Override
	public Org treeRoot(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		return orgDao.tree(params);
	}

	@Override
	public Org tree(Long siteId, Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("id", id);
		return orgDao.tree(params);
	}

	@Override
	public Org tree(Long siteId, Long id, int... depths) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("id", id);
		params.put("depthList", Arrays.asList(depths));
		return orgDao.tree(params);
	}

	@Override
	public ResultSearchResult<Org> search(Long siteId,	SearchArgumentSupport argumentSupport) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		return orgDao.search(params, argumentSupport);
	}
	
	public void setOrgDao(OrgDao orgDao) {
		this.orgDao = orgDao;
	}
}
